package cn.cloud9.opencloud.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置JSON转换
 * @author OnCloud9
 * @description
 * @project Open-Cloud
 * @date 2022年04月16日 15:33
 */
@Configuration
public class JsonConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建转换器Bean
        final FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        // JSON配置项Bean
        final FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                // 美化格式特性
                SerializerFeature.PrettyFormat,
                // 禁用循环引用侦测特性
                SerializerFeature.DisableCircularReferenceDetect
        );

        // 设置支持的类型？
        final ArrayList<MediaType> supportMediaTypeList = new ArrayList<>();
        supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        supportMediaTypeList.add(MediaType.TEXT_HTML);
        converter.setSupportedMediaTypes(supportMediaTypeList);

        // 加入转换器列表，设置在第一个上面
        converters.add(0, converter);
    }

}
